package com.question.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.achievement.dao.SubjectRepository;
import com.achievement.entity.Subject;
import com.common.util.TablePage;
import com.paper.dao.SubtopicsRepository;
import com.question.dao.OptionsRepository;
import com.question.dao.SingleRepository;
import com.question.entity.Options;
import com.question.entity.Single;
import com.question.entity.dto.SingleDTO;
@Service
@Transactional
public class SingleService implements ISingleService {
	@Autowired
	private SingleRepository singleDao;
	@Autowired 
	private SubjectRepository subjectDao;
	@Autowired
	private OptionsRepository optionsDao;
	@Autowired
	private SubtopicsRepository subtopicsDao;

	public List<SingleDTO> findAll() {
		List<SingleDTO> result = new ArrayList<SingleDTO>();
		List<Single> list = (List<Single>) singleDao.findAll();
		for(Single s:list) {
			String subject = "";
			if(s.getSubject()!=null)subject = s.getSubject().getName();
			result.add(new SingleDTO(s, subject));
		}
		return result;
	}


	public void saveOrUpdate(Single single) {
		/*if(null==single.getId()) {
			//新建
			Map<String, Options> options = single.getOptions();
			
			//答案选项的编号
			String answerCode = "";
			
			for(String s:options.keySet()) {
				String code = UUID.randomUUID().toString();
				options.get(s).setNum(code);
				if(s.equals(answerKey))answerCode = code;
				optionsDao.save(options.get(s));
				options.put(s, optionsDao.findByCode(code));
			}
			single.setOptions(options);
			single.setAnswer(optionsDao.findByCode(answerCode));
			
			Subject subject = new Subject();
			if(single.getSubject()!=null)subject = subjectDao.findOne(single.getSubject().getId());
			single.setSubject(subject);
			
			singleDao.save(single);
		}else {
			
		}*/
		Map<String, Options> options = single.getOptions();
		System.out.println("选项数"+options.size());
		System.out.println(options.keySet());
		Options answer = single.getAnswer();
		
		for(String s:options.keySet()) {
			if(answer.getNum().equals(options.get(s).getNum())) {
				System.out.println("选项相等");
				single.setAnswer(single.getOptions().get(s));
			}
		}
		
		singleDao.save(single);
		
	}

	public boolean delete(Long id) {
		try {
			singleDao.delete(id);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public TablePage<SingleDTO> findPage(Specification<Single> spec,Pageable pageable) {
		Page<Single> page = singleDao.findAll(spec, pageable);
		List<SingleDTO> list = new ArrayList<SingleDTO>();
		for(Single s:page.getContent()) {
			String subject = "";
			if(s.getSubject()!=null)
				subject = s.getSubject().getName();
			list.add(new SingleDTO(s, subject));
		}
		Page<SingleDTO> result = new PageImpl<>(list, pageable, page.getTotalElements());
		return TablePage.getTablePage(result);
	}


	public List<Single> findAll2() {
		
		return (List<Single>) singleDao.findAll();
	}


	public TablePage<Single> findRealPage(Specification<Single> spec, Pageable pageable) {
		Page<Single> page = singleDao.findAll(spec, pageable);
		return TablePage.getTablePage(page);
	}


	@Override
	public Single findOne(Long id) {
		// TODO Auto-generated method stub
		return singleDao.findOne(id);
	}


	@Override
	public void testSave() {
		// TODO Auto-generated method stub
		Single single = new Single();
		Options option = new Options();
		option.setNum("001");
		option.setContent("测试选项");
		single.setNum("006");
		single.setTitle("测试保存");
		single.getOptions().put("A", option);
		single.setAnswer(option);
		singleDao.save(single);
	}


	@Override
	public boolean isNumExist(Single single) {
		// TODO Auto-generated method stub
		Single s = singleDao.findByNum(single.getNum());
		if(null!=s&&s.getId()!=single.getId())return true;
		return false;
	}


	@Override
	public boolean isUsing(Long id) {
		// TODO Auto-generated method stub
		if(subtopicsDao.findBySingleId(id).size()>0)
			return true;
		return false;
	}

}
